import click
from sayhello import db,app
from sayhello.models import Message

@app.cli.command()  # 注册为命令
@click.option('--drop', is_flag=True, help='Create after drop')
def initdb(drop):
    if drop:
        db.drop_all()
    db.create_all()
    click.echo('Initalized database')

@app.cli.command()
@click.option('--count',default=20,help='Quantity of messages,default-20')
def forge(count):
    from faker import Faker
    db.drop_all()
    db.create_all()
    fake = Faker()
    click.echo('Working...')
    
    for i in range(count):
        message = Message(
			name=fake.name(),
			body=fake.sentence(),
   			timestamp=fake.date_time_this_year()
		)
        db.session.add(message)
    db.session.commit()
    click.echo('Created %d fake messages'%count)